﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Collections;
using System.IO;
using System.Data.SqlClient;
using System.Text;
using System.Threading;
using YunShop_WebAPI.Model;

namespace YunShop_WebAPI.BLL
{
    public class CloudPosService
    {
        public void SaveServiceLog(string FSendStr, string FResultStr, int FResultFlag, int FDataType)
        {
            try
            {
                string msql = string.Format(@"Insert Into [dbo].[t_AppServiceLog]([FDateTime] ,[FDataType] ,[FSendStr] ,[FResultStr],[FResultFlag])
                                    Values(getdate(),{0},'{1}','{2}',{3})", FDataType, FSendStr, FResultStr, FResultFlag);

                //DBHelper.ExeSQL(msql);
            }
            catch (SqlException ex)
            {
            }
        }

        public static int GetMaxAutoID(string strTablename, string strFieldname, string connstr)
        {
            string sql = string.Format(@"exec pr_bi_GetMaxAutoIDEx '{0}','{1}'", strTablename, strFieldname);
            //string connstr = SystemInfo.GetClusterConn(corpId);
            ArrayList dataAll = DBHelper.Select(sql, connstr);
            if (dataAll.Count > 0)
            {
                Hashtable record = (Hashtable)dataAll[0];
                return CommonFun.ConvertToInt(record["FLastID"]);
            }
            else
            {
                return 0;
            }
        }


        //用户表，把密码转成明码
        public ArrayList ConvertUserPsd(ArrayList mList, int mDataType)
        {
            if (mDataType != 3) return mList;
            ArrayList data = new ArrayList();
            foreach (Hashtable row in mList)
            {
                Hashtable record = row;
                if (record == null) continue;
                record["FPassword"] = Common.PosDecrypt(CommonFun.ConvertToString(row["FPassword"]));
                data.Add(record);
            }
            return data;
        }

        /// <summary>
        /// 操作员登录
        /// </summary>
        /// <returns></returns>
        public ArrayList UserLogin(UserLoginRequest request)
        {
            string dataInfo = Common.ConvertBase64StringToString(request.DataInfo);
            ArrayList sDataRows = (ArrayList)PluSoft.Utils.JSON.Decode(Common.GetValidJson(dataInfo));
            Hashtable row = sDataRows[0] as Hashtable;
            var msql = @"exec pr_Pos_UserLogin @@mUserNumber,@mUserPsd,@mTerminalNo";
            SqlParameterList par_list = new SqlParameterList();
            par_list.Add("@@mUserNumber", SqlDbType.VarChar, 100, CommonFun.ConvertToString(row["user_no"]));
            par_list.Add("@mUserPsd", SqlDbType.VarChar, 100, Common.PosEncrypt(CommonFun.ConvertToString(row["user_password"])));
            par_list.Add("@mTerminalNo", SqlDbType.VarChar, 100, "Pad");
            ArrayList mdata = DBHelper.DataTable2ArrayList(DBHelper.GetDataTable(msql, par_list.Get_List(),DBHelper.ConnMaster));
            mdata = ConvertUserPsd(mdata, 3);
            return mdata;
        }

        /// <summary>
        /// 商品表
        /// </summary>
        /// <returns></returns>
        public ArrayList GetPosBaseInfo(PrPosDataRequest request, ref CountPosDataModel count)
        {
            string dataInfo = Common.ConvertBase64StringToString(request.DataInfo);
            ArrayList sDataRows = (ArrayList)PluSoft.Utils.JSON.Decode(Common.GetValidJson(dataInfo));
            Hashtable row = sDataRows[0] as Hashtable;
            string connstr = SystemInfo.GetClusterConn(CommonFun.ConvertToInt(row["FCorpID"]));

            var msql = @"exec dbo.pr_cp_GetPosBaseInfoEx @hPageSize,@hPageIndex,@hLastID,@hCorpID,@hStockID,@hUserID,@hLastTime,@hDatatype";
            SqlParameterList par_master = new SqlParameterList();
            par_master.Add("@hPageSize", SqlDbType.Int, 200);
            par_master.Add("@hLastID", SqlDbType.Int, CommonFun.ConvertToInt(row["FLastID"]));
            par_master.Add("@hCorpID", SqlDbType.Int, CommonFun.ConvertToInt(row["FCorpID"]));
            par_master.Add("@hStockID", SqlDbType.Int, CommonFun.ConvertToInt(row["FStockID"]));
            par_master.Add("@hUserID", SqlDbType.Int, CommonFun.ConvertToInt(row["FUserID"]));
            par_master.Add("@hLastTime", SqlDbType.BigInt, CommonFun.ConvertToBigInt(row["FLastTime"]));
            par_master.Add("@hDatatype", SqlDbType.Int, CommonFun.ConvertToInt(row["FDatatype"]));//信息类型
            par_master.Add("@hPageIndex", SqlDbType.Int, CommonFun.ConvertToInt(row["FPageIndex"]));//信息类型
            DataSet ds = DBHelper.GetDataSet(msql, par_master.Get_List(), connstr);
            ArrayList mdata = ConvertUserPsd(DBHelper.DataTable2ArrayListNotNull(ds.Tables[0]), CommonFun.ConvertToInt(row["FDatatype"]));

            ArrayList mdata1 = DBHelper.DataTable2ArrayListNotNull(ds.Tables[1]);
            count.frowcount = mdata1.Count;//总记录数
            count.fpagesize = CommonFun.ConvertToInt(row["FPageSize"]);
            count.fpagecount = 1;
            count.fpageindex = CommonFun.ConvertToInt(row["FPageIndex"]);
            count.flasttime = 0;

            if (mdata1.Count > 0)
            {
                Hashtable row1 = mdata1[0] as Hashtable;
                if (row1.Contains("FRowCount")) count.frowcount = CommonFun.ConvertToInt(row1["FRowCount"]);//总记录数                
                if (row1.Contains("FPageCount")) count.fpagecount = CommonFun.ConvertToInt(row1["FPageCount"]);
                if (row1.Contains("FLastTime")) count.flasttime = CommonFun.ConvertToInt(row1["FLastTime"]);
            }
            return mdata;
        }

        public void SetCloudPosDataModel(ref CloudPosDataModel mResult)
        {
            mResult.FBillID = 0;
            mResult.FCBillID = 0;
            mResult.FMessage = "";
            mResult.FMemberID = 0;
            mResult.FMemberAmt = 0;
            mResult.FMemberPoint = 0;
            mResult.FClassID = 0;
        }

        public Boolean CheckPosBill(String sThreadID, ref CloudPosDataModel mResult,int mCorpID)
        {
            SetCloudPosDataModel(ref mResult);
            string connstr = SystemInfo.GetClusterConn(mCorpID);
            string msql = string.Format("Exec pr_cp_CheckPosBill '{0}'", sThreadID);
            ArrayList dataCheck = DBHelper.Select(msql, connstr);
            if (dataCheck.Count > 0)
            {
                Hashtable ht_check = dataCheck[0] as Hashtable;
                mResult.FBillID = CommonFun.ConvertToBigInt(ht_check["FBillID"]);
                mResult.FCBillID = CommonFun.ConvertToBigInt(ht_check["FCBillID"]);
                mResult.FMessage = "";
                mResult.FClassID = CommonFun.ConvertToBigInt(ht_check["FClassID"]);

                Int64 mMemberID = CommonFun.ConvertToBigInt(ht_check["FMemberID"]);
                if (mMemberID > 0)
                {
                    msql = string.Format(@"exec pr_cp_POS_vipinfo {0}", mMemberID);
                    DataTable dt = DBHelper.GetDataTable(msql, connstr);
                    if (dt.Rows.Count > 0)
                    {
                        mResult.FMemberID = mMemberID;
                        mResult.FMemberAmt = CommonFun.ConvertToDecimal(dt.Rows[0]["FAmount"]);
                        mResult.FMemberPoint = CommonFun.ConvertToDecimal(dt.Rows[0]["FPoint"]);
                    }
                }

                return true;
            }
            else
            {
                return false;
            }
        }

        public Boolean CheckBillSave(Int64 sThreadID,int mCorpID, Int64 mUserID, ref BillOrderModel mBillOrder)
        {
            string msql = "Select FTimestamp,FBillNo,KInterID From t_BillData_Master where FTimestamp = {0} and FUserID = {1} and FTransferFlag = 1";
            msql = string.Format(msql, sThreadID, mUserID);
            string connstr = SystemInfo.GetClusterConn(mCorpID);
            ArrayList dataCheck = DBHelper.Select(msql, connstr);
            if (dataCheck.Count > 0)
            {
                Hashtable ht_check = dataCheck[0] as Hashtable;
                mBillOrder.KInterID = CommonFun.ConvertToBigInt(ht_check["KInterID"]);
                mBillOrder.FBillNo = CommonFun.ConvertToString(ht_check["FBillNo"]);
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 获取交班单号
        /// </summary>
        /// <returns></returns>
        public Int64 GetPosClass(PrPosDataRequest request, ref PosClassModel mPosClass)
        {
            string dataInfo = Common.ConvertBase64StringToString(request.DataInfo);
            ArrayList sDataRows = (ArrayList)PluSoft.Utils.JSON.Decode(Common.GetValidJson(dataInfo));
            Hashtable row = sDataRows[0] as Hashtable;
            int mCorpID = CommonFun.ConvertToInt(row["FCorpID"]);
            Int64 mStockID = CommonFun.ConvertToBigInt(row["FStockID"]);
            Int64 mUserID = CommonFun.ConvertToBigInt(row["FUserID"]);
            Int64 mClassID = CommonFun.ConvertToBigInt(row["FClassID"]);

            return GetPosClassBill(mCorpID, mStockID, mUserID, mClassID, ref mPosClass);
        }

        /// <summary>
        /// 获取交班单号
        /// </summary>
        /// <returns></returns>
        public String GetMainWebUrl(PrPosDataRequest request, ref bool mResult)
        {
            string dataInfo = Common.ConvertBase64StringToString(request.DataInfo);
            ArrayList sDataRows = (ArrayList)PluSoft.Utils.JSON.Decode(Common.GetValidJson(dataInfo));
            Hashtable row = sDataRows[0] as Hashtable;
            int mCorpID = CommonFun.ConvertToInt(row["FCorpID"]);
            Int64 mUserID = CommonFun.ConvertToBigInt(row["FUserID"]);
            String mPassword = Common.PosEncrypt(CommonFun.ConvertToString(row["FPassword"]));
            string connstr = SystemInfo.GetClusterConn(mCorpID);

            String msql = "Exec pr_cp_GetMainWebUrl {0},{1},'{2}'";
            msql = string.Format(msql, mUserID, mCorpID, mPassword);
            DataTable dt = DBHelper.GetDataTable(msql, connstr);
            if (dt.Rows.Count > 0) mResult = true;
            ArrayList data = DBHelper.DataTable2ArrayList(dt);
            String json = PluSoft.Utils.JSON.Encode(data);
            json = Common.PosEncrypt(json);
            byte[] bytes = Encoding.UTF8.GetBytes(json);
            json = Convert.ToBase64String(bytes);
            //Service.HandlerErr(json);
            return json;
        }

        /// <summary>
        /// 获取交班单号
        /// </summary>
        /// <param name="sThreadID"></param>
        /// <param name="mResult"></param>
        /// <returns></returns>
        public Int64 GetPosClassBill(int mCorpID, Int64 mStockID, Int64 mUserID, Int64 mClassID, ref PosClassModel mResult)
        {
            mResult.KInterID = 0;
            mResult.FBillNo = "";
            string connstr = SystemInfo.GetClusterConn(mCorpID);
            string msql = string.Format("Exec pr_cp_GetPosClass {0},{1},{2},{3}", mCorpID, mStockID, mUserID, mClassID);
            DataTable dt = DBHelper.GetDataTable(msql, connstr);
            if (dt.Rows.Count > 0)
            {
                mResult.KInterID = CommonFun.ConvertToBigInt(dt.Rows[0]["KInterID"]);
                mResult.FBillNo = CommonFun.ConvertToString(dt.Rows[0]["FBillNo"]);
                return CommonFun.ConvertToBigInt(dt.Rows[0]["KInterID"]);
            }
            else
            {
                return 0;
            }
        }

        /// <summary>
        /// 获取交班单号
        /// </summary>
        /// <param name="sThreadID"></param>
        /// <param name="mResult"></param>
        /// <returns></returns>
        public Int64 GetBillOrderNo(PrPosDataRequest request, ref BillOrderModel mBillOrder)
        {
            string dataInfo = Common.ConvertBase64StringToString(request.DataInfo);
            ArrayList sDataRows = (ArrayList)PluSoft.Utils.JSON.Decode(Common.GetValidJson(dataInfo));
            Hashtable row = sDataRows[0] as Hashtable;
            int mCorpID = CommonFun.ConvertToInt(row["FCorpID"]);
            Int64 mUserID = CommonFun.ConvertToBigInt(row["FUserID"]);
            string connstr = SystemInfo.GetClusterConn(mCorpID);

            mBillOrder.KInterID = 0;
            mBillOrder.FBillNo = "";
            string msql = string.Format("Exec pr_cp_GetBillOrderNo 5,{0},{1}", mCorpID, mUserID);
            DataTable dt = DBHelper.GetDataTable(msql, connstr);
            if (dt.Rows.Count > 0)
            {
                mBillOrder.KInterID = CommonFun.ConvertToBigInt(dt.Rows[0]["FEntryID"]);
                mBillOrder.FBillNo = CommonFun.ConvertToString(dt.Rows[0]["FBillNo"]);
                return CommonFun.ConvertToBigInt(dt.Rows[0]["FEntryID"]);
            }
            else
            {
                return 0;
            }
        }

        /// <summary>
        /// 零售单据保存
        /// </summary>
        /// <returns></returns>
        public Boolean SetPosBillData(PrPosDataRequest request, ref CloudPosDataModel mResult)
        {
            String msg = "没有可保存的数据！";
            try
            {                
                string dataInfo = Common.ConvertBase64StringToString(request.DataInfo);
                SaveServiceLog(dataInfo, "SetPosBillData", 0, 88);
                ArrayList rows_all = (ArrayList)PluSoft.Utils.JSON.Decode(Common.GetValidJson(dataInfo));
                string msql = "";
                if (rows_all.Count == 0)
                {
                    mResult.FMessage = "没有可保存的数据！";
                    return false;
                }

                Hashtable row_pos = rows_all[0] as Hashtable;
                ArrayList rows_master = (ArrayList)PluSoft.Utils.JSON.Decode(Common.GetValidJson(CommonFun.ConvertToString(row_pos["PosMaster"])));
                ArrayList rows_detail = (ArrayList)PluSoft.Utils.JSON.Decode(Common.GetValidJson(CommonFun.ConvertToString(row_pos["PosDetail"])));
                ArrayList rows_pay = (ArrayList)PluSoft.Utils.JSON.Decode(Common.GetValidJson(CommonFun.ConvertToString(row_pos["PosPay"])));

                Hashtable row_master = rows_master[0] as Hashtable;

                string sBillNo = CommonFun.ConvertToString(row_master["FBillNo"]);
                int mCorpID = CommonFun.ConvertToInt(row_master["FCorpID"]);
                Int64 mStockID = CommonFun.ConvertToBigInt(row_master["FStockID"]);
                Int64 mUserID = CommonFun.ConvertToBigInt(row_master["FUserID"]);

                //处理旧版本的FBillKey问题
                if (CommonFun.ConvertToString(row_master["FBillKey"]).Length < 21)
                {
                    row_master["FBillKey"] = CommonFun.ConvertToString(row_master["FBillKey"]) + "_" + sBillNo;
                }
                string connstr = SystemInfo.GetClusterConn(mCorpID);
                string sThreadID = CommonFun.ConvertToString(row_master["FBillKey"]);

                if (CheckPosBill(sThreadID, ref mResult, mCorpID)) return true;
                                              
                //会员消费
                string sMessage = "会员储值扣款失败！";
                if (!SetMemberConsume(row_master, rows_pay, ref sMessage))
                {
                    mResult.FMessage = sMessage;
                    return false;
                }

                PosClassModel mClassModel = new PosClassModel();               

                SqlConnection con = new SqlConnection(connstr);
                con.Open();
                SqlTransaction tran = con.BeginTransaction();
                try
                {
                    //AppPosService AppPs = new AppPosService();
                    Int64 mKInterID = 0;// AppPs.GetPosMaxID();
                    Int64 mClassID = CommonFun.ConvertToBigInt(row_master["FClassID"]);
                    if (mClassID == 0) mClassID = GetPosClassBill(mCorpID, mStockID, mUserID, 0, ref mClassModel);

                    string msql_master = @"Insert into t_CloudPos_Master(FBillKey,KInterID, FBillNo, FDate, FClassID, FUserID, FCorpID, FStockID,  
                        FMemberID, FQty, FAmount, FRealAmount, FCostAmount,FSubs_InterID,FPoint,FCheckFlag,FEmpID,FBatchID,FTransferFlag,FTimeStamp)
	                    values(@FBillKey,{0}, @FBillNo, @FDate, {1}, @FUserID, @FCorpID, @FStockID, 
                        @FMemberID, @FQty, @FAmount, @FRealAmount, @FCostAmount,@KInterID,@FPoint,0,@FEmItemID,0,0,dbo.fn_GetTimestamp(GETDATE()))";
                    msql_master = string.Format(msql_master, mKInterID, mClassID);

                    SqlCommand cmd0 = new SqlCommand(msql_master, con);
                    cmd0.Connection = con;
                    cmd0.Transaction = tran;
                    cmd0.Parameters.Clear();
                    if (cmd0 != null) DBHelper.SetArgs(msql_master, row_master, cmd0);
                    cmd0.ExecuteNonQuery();

                    msql = @"Insert into t_CloudPos_Detail(MInterID, FGoodsID,mFSubItemID, mFQty, mFPrice, mFActualPrice, mFAmount, "
                        + "mFActualAmount, mFCostPrice, mFDataType,mFConvert,mFEmpID,mFBatchID,mFTransferFlag)values({0}, @FGoodsID,@FGoodsID, @FQty, "
                        + "@FPrice,@FActualPrice, @FAmount, @FActualAmount, 0, @FDataType,1,@FEmItemID,0,0)";
                    msql = string.Format(msql, mKInterID);

                    foreach (Hashtable row_detail in rows_detail)
                    {
                        SqlCommand cmd1 = new SqlCommand(msql, con);
                        cmd1.Connection = con;
                        cmd1.Transaction = tran;
                        cmd1.Parameters.Clear();
                        if (row_detail != null) DBHelper.SetArgs(msql, row_detail, cmd1);
                        cmd1.ExecuteNonQuery();
                    }

                    msql = @"Insert into t_CloudPos_Pay(MInterID, FPayID, mFAmount, mFPayAmount,mFBatchID,mFTransferFlag)"
                            +"values({0}, @FPayID, @FAmount, @FPayAmount,0,0)";
                    msql = string.Format(msql, mKInterID);

                    foreach (Hashtable row_pay in rows_pay)
                    {
                        SqlCommand cmd2 = new SqlCommand(msql, con);
                        cmd2.Connection = con;
                        cmd2.Transaction = tran;
                        cmd2.Parameters.Clear();
                        if (row_pay != null) DBHelper.SetArgs(msql, row_pay, cmd2);
                        cmd2.ExecuteNonQuery();
                    }                   

                    tran.Commit();
                    
                    A_UpPosDataOne(mKInterID, mClassID, mUserID, mCorpID);                    

                    if (CheckPosBill(sThreadID, ref mResult, mCorpID)) return true;

                    return false;
                }
                catch (SqlException ex)
                {
                    tran.Rollback();
                    msg = "错误：" + ex.Message.Replace("\"","").Replace("'","");
                    mResult.FMessage = msg;
                    return false;
                }
                finally
                {
                    con.Close();
                    con = null;
                }
            }
            catch (SqlException ex)
            {
                msg = "错误：" + ex.Message.Replace("\"", "").Replace("'", "");
                mResult.FMessage = msg;
                return false;
            }
        }

        private int A_UpPosDataOne(Int64 hInterID, Int64 hClassID, Int64 hUserID, int mCorpID)
        {
            try
            {
                int mHostID = 0;
                string connstr = SystemInfo.GetClusterConn(mCorpID);
                string mSystemSign = GetSystemSign(hUserID, mCorpID, ref mHostID);
                string sql = string.Format(@"Exec pr_ic_GetPosTransfer '{0}',{1},{2}", mSystemSign, mHostID, hInterID);//跟POS前台一样的结构
                DataSet ds_pos = DBHelper.GetDs(sql, connstr);
                DataTable dt_1 = ds_pos.Tables[0];
                DataTable dt_2 = ds_pos.Tables[1];
                DataTable dt_3 = ds_pos.Tables[2];

                DataSet ds = new DataSet();
                DataTable dt1 = dt_1.Copy();
                dt1.TableName = "Table1";

                DataTable dt2 = dt_2.Copy();
                dt2.TableName = "Table2";

                DataTable dt3 = dt_3.Copy();
                dt3.TableName = "Table3";

                ds.Tables.Add(dt1);
                ds.Tables.Add(dt2);
                ds.Tables.Add(dt3);

                if (ds.Tables[0].Rows.Count == 0) return 0;

                //YunWebService.POSWebServiceEx proxy = new YunWebService.POSWebServiceEx();
                //proxy.Url = System.Configuration.ConfigurationManager.AppSettings["YunShopUrl"]; ;
                //int iReturnID = proxy.SetPOSBillData(UnZipClass.ConvertDataSetToZipBuffer(ds), mSystemSign);

                //return iReturnID;

                return 0;
            }
            catch (Exception ex)
            {
                SaveServiceLog(ex.Message, "A_UpPosDataOne", 0, 88);
                return -1;
            }
        }

        /// <summary>
        /// 交班
        /// </summary>
        /// <returns></returns>
        public Int64 SetPosClass(PrPosDataRequest request, ref PosClassModel mPosClass)
        {
            string dataInfo = Common.ConvertBase64StringToString(request.DataInfo);
            ArrayList sDataRows = (ArrayList)PluSoft.Utils.JSON.Decode(Common.GetValidJson(dataInfo));
            Hashtable row = sDataRows[0] as Hashtable;
            Int64 mClassID = CommonFun.ConvertToBigInt(row["FClassID"]);
            Int64 mUserID = CommonFun.ConvertToBigInt(row["FUserID"]);
            int mCorpID = CommonFun.ConvertToInt(row["FCorpID"]);
            string connstr = SystemInfo.GetClusterConn(mCorpID);
            mPosClass.KInterID = 0;
            mPosClass.FBillNo = "";
            string msql = string.Format("Exec pr_cp_SetPosClass {0},{1}", mClassID, mUserID);
            DataTable dt = DBHelper.GetDataTable(msql,connstr);
            if (dt.Rows.Count > 0)
            {
                mPosClass.KInterID = CommonFun.ConvertToBigInt(dt.Rows[0]["KInterID"]);
                mPosClass.FBillNo = CommonFun.ConvertToString(dt.Rows[0]["FBillNo"]);
                return CommonFun.ConvertToBigInt(dt.Rows[0]["KInterID"]);
            }
            else
            {
                return 0;
            }
        }

        /// <summary>
        /// 交班
        /// </summary>
        /// <returns></returns>
        public Int64 SetStockCheckEnd(PrPosDataRequest request, ref PosClassModel mPosClass)
        {
            string dataInfo = Common.ConvertBase64StringToString(request.DataInfo);
            ArrayList sDataRows = (ArrayList)PluSoft.Utils.JSON.Decode(Common.GetValidJson(dataInfo));
            Hashtable rowmaster = sDataRows[0] as Hashtable;
            int mCorpID = CommonFun.ConvertToInt(rowmaster["FCorpID"]);
            Int64 mStockID = CommonFun.ConvertToBigInt(rowmaster["FStockID"]);
            Int64 mUserID = CommonFun.ConvertToBigInt(rowmaster["FUserID"]);
            int mDataType = CommonFun.ConvertToInt(rowmaster["FDataType"]);
            string connstr = SystemInfo.GetClusterConn(mCorpID);
            int sBatchID = GetMaxAutoID("y_Stc_Class", "FBatchID", connstr);

            mPosClass.KInterID = 0;
            mPosClass.FBillNo = "";
            string msql = string.Format("Exec pr_cp_SetStockCheckEnd {0},{1},{2},{3},{4}", mCorpID, mStockID, mUserID, mDataType, sBatchID);
            DataTable dt = DBHelper.GetDataTable(msql, connstr);
            if (dt.Rows.Count > 0)
            {
                mPosClass.KInterID = CommonFun.ConvertToBigInt(dt.Rows[0]["KInterID"]);
                mPosClass.FBillNo = CommonFun.ConvertToBigInt(dt.Rows[0]["KInterID"]).ToString();
                return CommonFun.ConvertToBigInt(dt.Rows[0]["KInterID"]);
            }
            else
            {
                return 0;
            }
        }

        //获取充值订单
        public static Int64 GetBillOrderNo(Int64 mUserID, int mCorpID)
        {
            try
            {
                string sql = @"exec pr_cp_GetBillOrderNo @mBillType,@mCorpID,@mUserID";
                SqlParameterList par_list1 = new SqlParameterList();
                par_list1.Add("@mBillType", SqlDbType.Int, 4, 5);
                par_list1.Add("@mCorpID", SqlDbType.Int, 4, mCorpID);
                par_list1.Add("@mUserID", SqlDbType.Int, 4, mUserID);
                string connstr = SystemInfo.GetClusterConn(mCorpID);
                DataTable dt = DBHelper.GetDataTable(sql, par_list1.Get_List(), connstr);

                if (dt.Rows.Count > 0) return CommonFun.ConvertToBigInt(dt.Rows[0]["FEntryID"]);
                //string sBillNo = CommFunc.ConvertDBNullToString(dr_order["FBillNo"]);//充值订单单号
                return 0;
            }
            catch (Exception E)
            {               
                return 0;
            }
        }

        /// <summary>
        /// 会员充值
        /// </summary>
        /// <param name="request"></param>
        /// <param name="mPosClass"></param>
        /// <returns></returns>
        public bool SetMemberDeposit(PrPosDataRequest request, ref MemberResultModel mPosClass)
        {
            try
            {
                string dataInfo = Common.ConvertBase64StringToString(request.DataInfo);
                //SaveServiceLog(dataInfo, "SetMemberDeposit", 0, 98);
                ArrayList sDataRows = (ArrayList)PluSoft.Utils.JSON.Decode(Common.GetValidJson(dataInfo));
                Hashtable row = sDataRows[0] as Hashtable;
                return SetMemberDeposit_Api(row, ref mPosClass);
            }
            catch (Exception ex) {
                SaveServiceLog(ex.Message, "SetMemberDeposit", 0, 98);
                return false;
            }
        }

        /// <summary>
        /// 会员充值退款
        /// </summary>
        /// <param name="request"></param>
        /// <param name="mPosClass"></param>
        /// <returns></returns>
        public bool SetMemberRefund(PrPosDataRequest request, ref MemberResultModel mPosClass)
        {
            try
            {
                string dataInfo = Common.ConvertBase64StringToString(request.DataInfo);
                ArrayList sDataRows = (ArrayList)PluSoft.Utils.JSON.Decode(Common.GetValidJson(dataInfo));
                Hashtable row = sDataRows[0] as Hashtable;
                return SetMemberRefund_Api(row, ref mPosClass);
            }
            catch (Exception ex)
            {
                SaveServiceLog(ex.Message, "SetMemberDeposit", 0, 98);
                return false;
            }
        }


        /// <summary>
        /// 会员余额扣款后的加密
        /// </summary>
        /// <returns></returns>
        public bool SetMemberConsume(Hashtable row_master, ArrayList rows_pay, ref string sMessage)
        {
            Int64 mMemberID = CommonFun.ConvertToBigInt(row_master["FMemberID"]);
            if (mMemberID == 0) return true;
            decimal mConsumeAmt = 0;
            foreach (Hashtable row in rows_pay)
            {
                if (CommonFun.ConvertToInt(row["FPayID"]) == 2)
                    mConsumeAmt += CommonFun.ConvertToDecimal(row["FAmount"]);
            }

            if (mConsumeAmt == 0) return true;

            Hashtable row_set = new Hashtable();
            row_set["FCorpID"] = CommonFun.ConvertToBigInt(row_master["FCorpID"]);
            row_set["FStockID"] = CommonFun.ConvertToBigInt(row_master["FStockID"]);
            row_set["FUserID"] = CommonFun.ConvertToBigInt(row_master["FUserID"]);
            row_set["FMemberID"] = CommonFun.ConvertToBigInt(row_master["FMemberID"]);
            row_set["FPayID"] = 0;
            row_set["FTimeID"] = 0;
            row_set["FGoodID"] = 0;
            row_set["FAmount"] = mConsumeAmt;
            row_set["FGiveAmt"] = 0;
            row_set["FQty"] = 0;
            row_set["FGiveQty"] = 0;
            row_set["FDataType"] = -1;
            row_set["FEmplID"] = 0;
            row_set["FObjectID"] = 0;
            MemberResultModel mPosClass = new MemberResultModel();
            bool bResult = SetMemberDeposit_Api(row_set, ref mPosClass);
            sMessage = mPosClass.FMessage;
            return bResult;
        }

        /// <summary>
        /// 会员资料保存
        /// </summary>
        /// <returns></returns>
        public ArrayList SetMemberInfo(PrPosDataRequest request, ref bool bResult, ref string msg)
        {
            try
            {
                string dataInfo = Common.ConvertBase64StringToString(request.DataInfo);
                ArrayList sDataRows = (ArrayList)PluSoft.Utils.JSON.Decode(Common.GetValidJson(dataInfo));
                Hashtable row = sDataRows[0] as Hashtable;
                string msql = @"Exec pr_cp_CheckMemberInfo {0},{1},'{2}'";
                msql = string.Format(msql, CommonFun.ConvertToInt(row["FClassID"]),
                    CommonFun.ConvertToInt(row["FCorpID"]), CommonFun.ConvertToInt(row["FNumber"]));
                string connstr = SystemInfo.GetClusterConn(CommonFun.ConvertToInt(row["FCorpID"]));
                DataTable dtCheck = DBHelper.GetDataTable(msql, connstr);
                if (dtCheck.Rows.Count > 0) 
                {
                    row["FItemID"] = CommonFun.ConvertToBigInt(dtCheck.Rows[0]["FItemID"]);
                }                

                msql = string.Format(@"Exec pr_cp_GetMemberInfo {0},{1},'NothingInHere20210220001'", CommonFun.ConvertToInt(row["FCorpID"]), 0);
                DataTable dtSave = DBHelper.GetDataTable(msql, connstr);

                DataRow drNew = dtSave.NewRow();
                drNew["FCorpID"] = CommonFun.ConvertToInt(row["FCorpID"]);
                drNew["FItemID"] = CommonFun.ConvertToBigInt(row["FItemID"]);
                drNew["FClassID"] = CommonFun.ConvertToInt(row["FClassID"]);
                drNew["FNumber"] = CommonFun.ConvertToString(row["FNumber"]);
                drNew["FName"] = CommonFun.ConvertToString(row["FName"]);
                drNew["FDiscount"] = CommonFun.ConvertToDecimal(row["FDiscount"]);
                drNew["FEndTime"] = CommonFun.ConvertToDateTime(row["FEndTime"]);
                drNew["FBirthday"] = CommonFun.ConvertToDateTime(row["FBirthday"]);
                drNew["FPhone"] = CommonFun.ConvertToString(row["FPhone"]);
                drNew["FEmail"] = CommonFun.ConvertToString(row["FEmail"]);
                drNew["FAddress"] = CommonFun.ConvertToString(row["FAddress"]);
                drNew["FRemark"] = CommonFun.ConvertToString(row["FRemark"]);
                drNew["FPoint"] = 0;
                drNew["FPointFlag"] = 1;
                drNew["FAmount"] = 0;
                drNew["FAmountFlag"] = 1;
                drNew["FQty"] = 0;
                drNew["FDeleted"] = 1;
                drNew["FPassword"] = "";
                drNew["FStartTime"] = DateTime.Now;
                drNew["FParentID"] = 0;
                drNew["FSex"] = CommonFun.ConvertToDecimal(row["FSex"]);
                drNew["FParentNumber"] = CommonFun.ConvertToString(row["FParentNumber"]);
                dtSave.Rows.Add(drNew);
                DataSet ds = new DataSet();
                DataTable dt1 = dtSave.Copy();
                ds.Tables.Add(dt1);
                DataTable dt_ret = new DataTable();
                if (dtCheck.Rows.Count == 0)
                {
                    POSWebServiceVip.POSWebServiceVip service = new POSWebServiceVip.POSWebServiceVip();
                    service.Url = System.Configuration.ConfigurationManager.AppSettings["MemberHostUrl"];
                    byte[] zipBuffer = UnZipClass.ConvertDataSetToZipBuffer(ds);

                    int mHostID = 0;
                    string mSystemSign = GetSystemSign(CommonFun.ConvertToInt(row["FUserID"]), CommonFun.ConvertToInt(row["FCorpID"]), ref mHostID);
                    byte[] retbuffer = service.SetPosMemberDataEx(zipBuffer, mSystemSign);
                    if (retbuffer == null)
                    {
                        return null;
                    }

                    DataSet retDs = UnZipClass.ConvertZipBufferToDataSet(retbuffer);
                    dt_ret = retDs.Tables[0];
                }
                else {
                    msql = @"Exec pr_cp_GetMemberInfo {0},{1},'{2}'";
                    msql = string.Format(msql, CommonFun.ConvertToInt(row["FCorpID"]),
                        CommonFun.ConvertToBigInt(dtCheck.Rows[0]["FItemID"]), CommonFun.ConvertToInt(row["FNumber"]));
                    dt_ret = DBHelper.GetDataTable(msql, connstr);
                }
                ArrayList mdata = DBHelper.DataTable2ArrayList(dt_ret);
                if ((mdata.Count > 0) && (dt_ret.Rows.Count > 0))
                {
                    if (CommonFun.ConvertToBigInt(dt_ret.Rows[0]["FItemID"]) > 0)
                    {
                        drNew["FItemID"] = CommonFun.ConvertToBigInt(dt_ret.Rows[0]["FItemID"]);
                        mdata = DBHelper.DataTable2ArrayList(dtSave);
                        bResult = true;
                    }
                    else {
                        msg = CommonFun.ConvertToString(dt_ret.Rows[0]["FRemark"]);
                    }
                }
                return mdata;
            }
            catch (Exception ex)
            {
                //Service.HandlerErr(ex.Message);
                return null;
                //WriteError(ex.Message);
            }
        }


        public string GetSystemSign(Int64 mUserID, int mCorpID, ref int mHostID)
        {
            string connstr = SystemInfo.GetClusterConn(mCorpID);
            string msql = @"Exec pr_cp_GetSystemSign {0},'{1}','{2}'";
            msql = string.Format(msql, mUserID, CommonFun.GetTimeStamp(), mCorpID);
            DataTable dt = DBHelper.GetDataTable(msql, connstr);
            DataRow dr = dt.Rows[0];

            mHostID = CommonFun.ConvertToInt(dr["FHostID"]);

            Hashtable result = new Hashtable();
            result["FHostID"] = mHostID;
            result["FIsMain"] = CommonFun.ConvertToInt(dr["FIsMain"]);
            result["FHostNumber"] = CommonFun.ConvertToString(dr["FHostNumber"]);
            result["FUserID"] = CommonFun.ConvertToBigInt(dr["FUserID"]);
            result["FCorpID"] = CommonFun.ConvertToBigInt(dr["FCorpID"]);
            result["FStockID"] = CommonFun.ConvertToBigInt(dr["FStockID"]);
            result["FTimeStamp"] = CommonFun.GetTimeStamp();
            string json = PluSoft.Utils.JSON.Encode(result);
            return System.Web.HttpUtility.UrlEncode(Security.Encrypt(json));           
        }

        /// <summary>
        /// 读单个会员资料
        /// </summary> 
        /// <returns></returns>
        public ArrayList GetMemberInfo_Api(PrPosDataRequest request, ref bool bResult)
        {
            try
            {
                string dataInfo = Common.ConvertBase64StringToString(request.DataInfo);
                ArrayList sDataRows = (ArrayList)PluSoft.Utils.JSON.Decode(Common.GetValidJson(dataInfo));
                Hashtable row = sDataRows[0] as Hashtable;
                string connstr = SystemInfo.GetClusterConn(CommonFun.ConvertToInt(row["FCorpID"]));
                string SystemID = CommonFun.ConvertToBigInt(row["FUserID"]).ToString() + ","
                    + CommonFun.ConvertToInt(row["FCorpID"]).ToString() + "," + "psd";//系统用户的身份
                string sSystemAppID = YunShopSecurity.Encrypt(SystemID);

                string msql = string.Format(@"Select '{0}' as FSystemAppID,'{1}' as FNumber",sSystemAppID,CommonFun.ConvertToString(row["FNumber"]));
                DataSet ds = DBHelper.GetDs(msql, connstr);

                POSWebServiceVip.POSWebServiceVip service = new POSWebServiceVip.POSWebServiceVip();
                service.Url = System.Configuration.ConfigurationManager.AppSettings["MemberHostUrl"];
                byte[] zipBuffer = UnZipClass.ConvertDataSetToZipBuffer(ds);

                int mHostID = 0;
                Int64 mUserID = CommonFun.ConvertToBigInt(row["FUserID"]);
                byte[] retbuffer = service.GetPosMemberInfo(zipBuffer, GetSystemSign(mUserID, CommonFun.ConvertToInt(row["FCorpID"]), ref mHostID));
                if (retbuffer == null) return null;

                DataSet retDs = UnZipClass.ConvertZipBufferToDataSet(retbuffer);
                DataTable dt_ret = retDs.Tables[0];
                ArrayList mdata = DBHelper.DataTable2ArrayList(dt_ret);
                if (mdata.Count > 0) bResult = true;
                return mdata;
            }
            catch (Exception ex)
            {
                //Service.HandlerErr(ex.Message);
                return null;
            }
        }

        /// <summary>
        /// 读商品资料库
        /// </summary>
        /// <returns></returns>
        public ArrayList GetSysGoodsInfo(PrPosDataRequest request, ref bool bResult)
        {
            try
            {
                string dataInfo = Common.ConvertBase64StringToString(request.DataInfo);
                ArrayList sDataRows = (ArrayList)PluSoft.Utils.JSON.Decode(Common.GetValidJson(dataInfo));
                Hashtable row = sDataRows[0] as Hashtable;

                string sCode = CommonFun.ConvertToString(row["FCode"]);

                string sql = string.Format("select '{0}' as FCode", CommonFun.ConvertToString(row["FCode"]));
                DataSet ds = DBHelper.GetDataSet(sql, null,DBHelper.ConnMaster);
                if (ds.Tables[0].Rows.Count == 0) return null;

                sysgoods.POSWebService service = new sysgoods.POSWebService();
                byte[] zipBuffer = UnZipClass.ConvertDataSetToZipBuffer(ds);
                byte[] retbuffer = service.ImportSysGood_Code(zipBuffer); 
                if (retbuffer == null) return null;

                DataSet retDs = UnZipClass.ConvertZipBufferToDataSet(retbuffer);
                DataTable dt_ret = retDs.Tables[0];
                ArrayList mdata = DBHelper.DataTable2ArrayList(dt_ret);
                if (mdata.Count > 0) bResult = true;
                return mdata;
            }
            catch (Exception ex)
            {
                //Service.HandlerErr(ex.Message);
                return null;
            }
        }

        /// <summary>
        /// 会员充值
        /// </summary>
        /// <returns></returns>
        public bool SetMemberDeposit_Api(Hashtable row, ref MemberResultModel mPosClass)
        {
            mPosClass.FMemberID = 0;
            mPosClass.FAmount = 0;
            mPosClass.FPoint = 0;
            mPosClass.FQty = 0;
            mPosClass.FStockQty = 0;
            mPosClass.FMessage = "";

            int mCorpID = CommonFun.ConvertToInt(row["FCorpID"]);
            Int64 mStockID = CommonFun.ConvertToBigInt(row["FStockID"]);
            Int64 mUserID = CommonFun.ConvertToBigInt(row["FUserID"]);
            String mRemark = "安卓前台会员充值";
            string connstr = SystemInfo.GetClusterConn(mCorpID);
            switch (CommonFun.ConvertToInt(row["FDataType"]))
            {
                case 3:
                    mRemark = "安卓前台会员次卡充值";
                    break;
                case -3:
                    mRemark = "安卓前台会员次卡消费";
                    break;
            }

            try
            {

                Int64 sBillOrderID = GetBillOrderNo(mUserID, mCorpID);//服务器获取充值订单

                PosClassModel mClassModel = new PosClassModel();
                Int64 mClassID = CommonFun.ConvertToBigInt(row["FClassID"]);
                if (mClassID == 0) mClassID = GetPosClassBill(mCorpID, mStockID, mUserID, 0, ref mClassModel);

                POSWebServiceVip.POSWebServiceVip service = new POSWebServiceVip.POSWebServiceVip();
                service.Url = System.Configuration.ConfigurationManager.AppSettings["MemberHostUrl"];

                string SystemID = mUserID.ToString() + "," + mCorpID.ToString() + "," + "psd";//系统用户的身份
                string sSystemAppID = YunShopSecurity.Encrypt(SystemID);

                string msql = @"Select @FBillID as FBillID,@FStockID as FStockID,@FMemberID as FMemberID,@FPayItemID as FPayID,@FAmount as FAmount,
                        @FGiveAmt as FGiveAmt,'{0}' as FSystemAppID,@FDataType as FDataType,@FQty as FQty,@FGiveQty as FGiveQty,@FTimeID as FTimeID,
                        0 as FGoodID,@FEmplID as FEmplID,@FClassID as FClassID,@FObjectID as FObjectID,@FRemark as FRemark, @FIsReturn as FIsReturn,0 as mSpareID,
                        @FBillID as FBillOrderID,'' as MemberNumber,@FPayOrderNo as FPayOrderNo";

                msql = string.Format(msql, sSystemAppID);

                SqlParameterList par_list = new SqlParameterList();
                par_list.Add("@FBillID", SqlDbType.BigInt, sBillOrderID);
                par_list.Add("@FCorpID", SqlDbType.Int, CommonFun.ConvertToBigInt(row["FCorpID"]));
                par_list.Add("@FStockID", SqlDbType.Int, CommonFun.ConvertToInt(row["FStockID"]));
                par_list.Add("@FUserID", SqlDbType.Int, CommonFun.ConvertToInt(row["FUserID"]));
                par_list.Add("@FMemberID", SqlDbType.Int, CommonFun.ConvertToInt(row["FMemberID"]));
                par_list.Add("@FPayItemID", SqlDbType.Int, CommonFun.ConvertToInt(row["FPayID"]));
                par_list.Add("@FTimeID", SqlDbType.Int, CommonFun.ConvertToInt(row["FTimeID"]));
                par_list.Add("@FGoodID", SqlDbType.Int, CommonFun.ConvertToInt(row["FGoodID"]));
                par_list.Add("@FAmount", SqlDbType.Decimal, CommonFun.ConvertToDecimal(row["FAmount"]));
                par_list.Add("@FGiveAmt", SqlDbType.Decimal, CommonFun.ConvertToDecimal(row["FGiveAmt"]));
                par_list.Add("@FQty", SqlDbType.Decimal, CommonFun.ConvertToDecimal(row["FQty"]));
                par_list.Add("@FGiveQty", SqlDbType.Decimal, CommonFun.ConvertToDecimal(row["FGiveQty"]));
                par_list.Add("@FRemark", SqlDbType.VarChar, mRemark);
                par_list.Add("@FDataType", SqlDbType.Int, CommonFun.ConvertToInt(row["FDataType"]));
                par_list.Add("@FEmplID", SqlDbType.Int, CommonFun.ConvertToInt(row["FEmplID"]));
                par_list.Add("@FClassID", SqlDbType.Int, mClassID);
                par_list.Add("@FObjectID", SqlDbType.Int, CommonFun.ConvertToInt(row["FObjectID"]));
                if (row.Contains("FPayOrderNo"))
                {
                    par_list.Add("@FPayOrderNo", SqlDbType.VarChar, CommonFun.ConvertToString(row["FPayOrderNo"]));
                }
                else {
                    par_list.Add("@FPayOrderNo", SqlDbType.VarChar, "");
                }
                par_list.Add("@FIsReturn", SqlDbType.Int, 0);

                int mHostID = 0;
                DataSet ds_dep = DBHelper.GetDataSet(msql, par_list.Get_List(), connstr);
                byte[] zipBuffer = UnZipClass.ConvertDataSetToZipBuffer(ds_dep);
                string mSystemSign = GetSystemSign(CommonFun.ConvertToInt(row["FUserID"]), CommonFun.ConvertToInt(row["FCorpID"]), ref mHostID);

                //建立订单
                byte[] retbuffer = service.SetPosRechargeOrder(zipBuffer, mSystemSign);
                if (retbuffer == null)
                {
                    mPosClass.FMessage = "获取订单失败";
                    return false;
                }
                DataTable dt = UnZipClass.ConvertZipBufferToDataTable(retbuffer);
                if (dt == null || dt.Rows.Count == 0)
                {
                    //查询订单
                    retbuffer = GetMemberDpOrderState(zipBuffer, mSystemSign);
                    DataSet ds = UnZipClass.ConvertZipBufferToDataSet(retbuffer);
                    if (ds.Tables.Count == 0)
                    {
                        mPosClass.FMessage = "查询订单失败";
                        return false;
                    }
                    dt = ds.Tables[0];
                }
                if (CommonFun.ConvertToInt(dt.Rows[0]["FCurrentStateID"]) < 0)
                {
                    mPosClass.FMessage = "获取订单状态失败";
                    return false;
                }

                byte[] buffer = service.SetMemberRechargeEx(zipBuffer, mSystemSign); //充值消费
                DataTable dt_dep = UnZipClass.ConvertZipBufferToDataTable(buffer);
                if (dt_dep == null || dt_dep.Rows.Count == 0)
                {
                    //查询订单
                    retbuffer = GetMemberDpOrderState(zipBuffer, mSystemSign);
                    DataSet ds = UnZipClass.ConvertZipBufferToDataSet(retbuffer);
                    if (ds.Tables.Count == 0)
                    {
                        mPosClass.FMessage = "查询订单失败";
                        return false;
                    }
                    dt_dep = ds.Tables[0];
                }

                DataRow dr = dt_dep.Rows[0];

                if (CommonFun.ConvertToInt(dr["FCurrentStateID"]) < 1)
                {
                    mPosClass.FMemberID = 0;
                    mPosClass.FAmount = 0;
                    mPosClass.FPoint = 0;
                    mPosClass.FQty = 0;
                    mPosClass.FStockQty = 0;
                    mPosClass.FMessage = CommonFun.ConvertToString(dr["FMessage"]);
                    return false;
                }

                mPosClass.FMemberID = CommonFun.ConvertToBigInt(dr["FItemID"]);
                mPosClass.FAmount = CommonFun.ConvertToDecimal(dr["FAmount"]);
                mPosClass.FPoint = CommonFun.ConvertToDecimal(dr["FPoint"]);
                mPosClass.FQty = CommonFun.ConvertToDecimal(dr["FQty"]);
                mPosClass.FStockQty = CommonFun.ConvertToDecimal(dr["FStockQty"]);
                mPosClass.FMessage = CommonFun.ConvertToString(dr["FMessage"]);
            }
            catch (Exception e) {
                SaveServiceLog(e.Message, "SetMemberDeposit_Api", 0, 99);
            }

            return true;
        }

        /// <summary>
        /// 会员充值退款
        /// </summary>
        /// <returns></returns>
        public bool SetMemberRefund_Api(Hashtable row, ref MemberResultModel mPosClass)
        {
            mPosClass.FMemberID = 0;
            mPosClass.FAmount = 0;
            mPosClass.FPoint = 0;
            mPosClass.FQty = 0;
            mPosClass.FStockQty = 0;
            mPosClass.FMessage = "";

            int mCorpID = CommonFun.ConvertToInt(row["FCorpID"]);
            Int64 mStockID = CommonFun.ConvertToBigInt(row["FStockID"]);
            Int64 mUserID = CommonFun.ConvertToBigInt(row["FUserID"]);
            Int64 sKInterID = 0;
            string connstr = SystemInfo.GetClusterConn(mCorpID);
            try
            {
                PosClassModel mClassModel = new PosClassModel();
                Int64 mClassID = GetPosClassBill(mCorpID, mStockID, mUserID, 0, ref mClassModel);

                POSWebServiceVip.POSWebServiceVip service = new POSWebServiceVip.POSWebServiceVip();
                service.Url = System.Configuration.ConfigurationManager.AppSettings["MemberHostUrl"];

                string SystemID = mUserID.ToString() + "," + mCorpID.ToString() + "," + "psd";//系统用户的身份
                string sSystemAppID = YunShopSecurity.Encrypt(SystemID);

                sKInterID = CommonFun.ConvertToBigInt(row["FBillID"]);
                string msql = @"Exec pr_ic_GetMemberRefundData '{0}',{1},{2}";
                msql = string.Format(msql, sSystemAppID, sKInterID, mClassID);
                DataSet ds = DBHelper.GetDs(msql, connstr);
                if (ds.Tables[0].Rows.Count == 0) return false;

                DataTable dt0 = ds.Tables[0];

                byte[] buffer = service.DepositeRefund(UnZipClass.ConvertDataSetToZipBuffer(ds));
                ds = UnZipClass.ConvertZipBufferToDataSet(buffer);                
                
                if (ds == null) return false;
                DataTable dt1 = ds.Tables[0];
                if (dt1 == null || dt1.Rows.Count == 0)
                {
                    mPosClass.FMessage = "退款返回失败";
                    return false;
                }
                if (CommonFun.ConvertToInt(dt1.Rows[0]["FCurrentStateID"]) < 0)
                {
                    mPosClass.FMessage = CommonFun.ConvertToString(dt1.Rows[0]["FMessage"]);
                    return false;
                }

                DataRow dr = dt1.Rows[0];
                mPosClass.FMemberID = CommonFun.ConvertToBigInt(dr["FItemID"]);
                mPosClass.FAmount = CommonFun.ConvertToDecimal(dr["FAmount"]);
                mPosClass.FPoint = CommonFun.ConvertToDecimal(dr["FPoint"]);
                mPosClass.FQty = CommonFun.ConvertToDecimal(dr["FQty"]);
                mPosClass.FStockQty = 0;// CommonFun.ConvertToDecimal(dr["FStockQty"]);
                mPosClass.FMessage = CommonFun.ConvertToString(dr["FMessage"]);
            }
            catch (Exception e)
            {
                SaveServiceLog(e.Message, "SetMemberDeposit_Api", 0, 99);
            }

            return true;
        }
                
        /// <summary>
        /// 检查订单状态
        /// </summary>
        /// <param name="zipBuffer"></param>
        /// <returns></returns>
        public static byte[] GetMemberDpOrderState(byte[] zipBuffer, string mSystemSign)
        {
            try
            {
                POSWebServiceVip.POSWebServiceVip service = new POSWebServiceVip.POSWebServiceVip();
                service.Url = System.Configuration.ConfigurationManager.AppSettings["MemberHostUrl"]; 
                service.Timeout = 10000;
                byte[] zipBuffer0 = null;
                int QueryCount = 0;
                while (zipBuffer0 == null && QueryCount < 5)
                {
                    zipBuffer0 = service.GetMemberDpOrderState(zipBuffer, mSystemSign);
                    QueryCount++;
                    Thread.Sleep(1000 * QueryCount);
                }
                return zipBuffer0;
            }
            catch (Exception E)
            {
                //writeServiceLog(E.Message, "DepositeOrderQuery", "16", "0");
                return null;
            }
        }
        
        /// <summary>
        /// 数据查询
        /// </summary>
        /// <returns></returns>
        public ArrayList GetPosDataList(PrPosDataRequest request, ref bool bResult)
        {
            try
            {
                string dataInfo = Common.ConvertBase64StringToString(request.DataInfo);
                //Service.HandlerErr(dataInfo);
                ArrayList sDataRows = (ArrayList)PluSoft.Utils.JSON.Decode(Common.GetValidJson(dataInfo));
                Hashtable row = sDataRows[0] as Hashtable;
                var msql = @"exec pr_cp_GetPosDataList @hPageIndex,@hPageSize,@hObjectID,@hCorpID,@hUserID,@hStockID,@hEntryID,@hNumber,@hDatatype";

                SqlParameterList par_list = new SqlParameterList();
                par_list.Add("@hPageIndex", SqlDbType.Int, CommonFun.ConvertToInt(row["FPageIndex"]));
                par_list.Add("@hPageSize", SqlDbType.Int, 500);
                par_list.Add("@hObjectID", SqlDbType.Int, CommonFun.ConvertToInt(row["FObjectID"]));
                par_list.Add("@hCorpID", SqlDbType.Int, CommonFun.ConvertToBigInt(row["FCorpID"]));
                par_list.Add("@hStockID", SqlDbType.Int, CommonFun.ConvertToInt(row["FStockID"]));
                par_list.Add("@hUserID", SqlDbType.Int, CommonFun.ConvertToBigInt(row["FUserID"]));
                par_list.Add("@hNumber", SqlDbType.VarChar, CommonFun.ConvertToString(row["FNumber"]));
                par_list.Add("@hDatatype", SqlDbType.Int, CommonFun.ConvertToInt(row["FDataType"]));
                par_list.Add("@hEntryID", SqlDbType.BigInt, CommonFun.ConvertToBigInt(row["FEntryID"]));
                string connstr = SystemInfo.GetClusterConn(CommonFun.ConvertToInt(row["FCorpID"]));
                if (CommonFun.ConvertToInt(row["FDataType"]) == 3)
                {
                    DataSet ds = DBHelper.GetDataSet(msql, par_list.Get_List(), connstr);
                    ArrayList mdata1 = DBHelper.DataTable2ArrayList(ds.Tables[1]);
                    if (mdata1.Count > 0) bResult = true;
                    return mdata1;
                }
                else
                {

                    ArrayList mdata = DBHelper.Select(msql, par_list.Get_List(), connstr);
                    if (mdata.Count > 0) bResult = true;
                    return mdata;
                }
            }
            catch (Exception ex)
            {
                //Service.HandlerErr(ex.Message);
                return null;
                //WriteError(ex.Message);
            }
        }

        /// <summary>
        /// 交班数据
        /// </summary>
        /// <returns></returns>
        public ArrayList GetPosClassList(PrPosDataRequest request, ref bool bResult)
        {
            string dataInfo = Common.ConvertBase64StringToString(request.DataInfo);
            ArrayList sDataRows = (ArrayList)PluSoft.Utils.JSON.Decode(Common.GetValidJson(dataInfo));
            Hashtable row = sDataRows[0] as Hashtable;
            var msql = @"exec pr_cp_GetPosClassList @hClassID,@hCorpID,@hStockID,@hUserID";
            string connstr = SystemInfo.GetClusterConn(CommonFun.ConvertToInt(row["FCorpID"]));
            SqlParameterList par_list = new SqlParameterList();
            par_list.Add("@hClassID", SqlDbType.BigInt, CommonFun.ConvertToBigInt(row["FClassID"]));
            par_list.Add("@hCorpID", SqlDbType.Int, CommonFun.ConvertToInt(row["FCorpID"]));
            par_list.Add("@hStockID", SqlDbType.Int, CommonFun.ConvertToInt(row["FStockID"]));
            par_list.Add("@hUserID", SqlDbType.Int, CommonFun.ConvertToBigInt(row["FUserID"]));
            ArrayList mdata = DBHelper.Select(msql, par_list.Get_List(), connstr);
            if (mdata.Count > 0) bResult = true;
            bResult = true;
            return mdata;
        }

        /// <summary>
        /// 商品分类保存
        /// </summary>
        /// <returns></returns>
        public ArrayList SetGoodsTypeInfo(PrPosDataRequest request, ref bool bResult, ref string msg)
        {
            try
            {
                string dataInfo = Common.ConvertBase64StringToString(request.DataInfo);
                //Service.HandlerErr(dataInfo);
                ArrayList sDataRows = (ArrayList)PluSoft.Utils.JSON.Decode(Common.GetValidJson(dataInfo));
                Hashtable row = sDataRows[0] as Hashtable;
                string msql = @"Exec [dbo].[pr_cp_SetGoodsTypeInfo] @FItemID,@FParentID,@FCorpID,@FNumber,@FName,@FFreshType,@FRemark";

                SqlParameterList detail_par_list = new SqlParameterList();
                detail_par_list.Add("@FItemID", SqlDbType.BigInt, CommonFun.ConvertToBigInt(row["FItemID"]));
                detail_par_list.Add("@FParentID", SqlDbType.Int, CommonFun.ConvertToBigInt(row["FParentID"]));
                detail_par_list.Add("@FCorpID", SqlDbType.Int, CommonFun.ConvertToInt(row["FCorpID"]));
                detail_par_list.Add("@FNumber", SqlDbType.VarChar, CommonFun.ConvertToString(row["FNumber"]));
                detail_par_list.Add("@FName", SqlDbType.VarChar, CommonFun.ConvertToString(row["FName"]));
                detail_par_list.Add("@FFreshType", SqlDbType.Int, CommonFun.ConvertToInt(row["FFreshType"]));
                detail_par_list.Add("@FRemark", SqlDbType.VarChar, "");
                string connstr = SystemInfo.GetClusterConn(CommonFun.ConvertToInt(row["FCorpID"]));
                ArrayList list_save = DBHelper.Select(msql, detail_par_list.Get_List(), connstr);
                if (list_save.Count > 0)
                {

                    Hashtable ht_save = list_save[0] as Hashtable;
                    if (CommonFun.ConvertToInt(ht_save["FItemID"]) > 0)
                    {
                        msql = string.Format("Exec [dbo].[pr_cp_GetGoodsTypeInfo] {0}", CommonFun.ConvertToInt(ht_save["FItemID"]));
                        ArrayList mdata = DBHelper.Select(msql, connstr);
                        if (mdata.Count > 0)
                        {
                            bResult = true;
                            return mdata;
                        }
                        else
                        {
                            msg = CommonFun.ConvertToString(ht_save["msg"]);
                        }
                    }
                }
                else {
                    msg = "保存失败。";
                }
                return null;
                
            }
            catch (Exception ex)
            {
                return null;
                //WriteError(ex.Message);
            }
        }

        public static string GetBillNumber(int mBillType, Int64 mCorpID, string connstr)
        {
            string sql = string.Format(@"EXEC pr_GetBillNumber {0},{1}", mBillType, mCorpID);

            ArrayList dataAll = DBHelper.Select(sql, connstr);
            if (dataAll.Count > 0)
            {
                Hashtable record = (Hashtable)dataAll[0];
                return CommonFun.ConvertToString(record["FBillNumber"]);
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// 单据的保存
        /// </summary>
        /// <returns></returns>
        public Int64 SetBillData(PrPosDataRequest request, ref BillOrderModel mBillOrder)
        {
            String msg = "没有可保存的数据！";
            try
            {
                string dataInfo = Common.ConvertBase64StringToString(request.DataInfo);
                ArrayList rows_master = (ArrayList)PluSoft.Utils.JSON.Decode(Common.GetValidJson(dataInfo));
                string msql = "";
                if (rows_master.Count == 0) return 0;

                Hashtable row_master = rows_master[0] as Hashtable;
                ArrayList rows_detail = (ArrayList)PluSoft.Utils.JSON.Decode(Common.GetValidJson(CommonFun.ConvertToString(row_master["DataDetail"])));                
                                
                int mCorpID = CommonFun.ConvertToInt(row_master["FCorpID"]);
                string connstr = SystemInfo.GetClusterConn(mCorpID);

                Int64 mStockID = CommonFun.ConvertToBigInt(row_master["FStockID"]);
                Int64 mUserID = CommonFun.ConvertToBigInt(row_master["FUserID"]);
                Int64 mKInterID = 0;
                int mDataType = CommonFun.ConvertToInt(row_master["FDataType"]);
                Int64 sFTimestamp = CommonFun.ConvertToBigInt(row_master["FTimestamp"]);
                if (CheckBillSave(sFTimestamp, mCorpID,mUserID, ref mBillOrder)) return sFTimestamp;
                
                string sBillNo = GetBillNumber(mDataType, mCorpID, connstr);

                switch (mDataType) { 
                    case 1:
                        mKInterID = GetMaxAutoID("y_Ins_Master", "KInterID", connstr);
                        break;
                    case 4:
                        mKInterID = GetMaxAutoID("y_Inv_Master", "KInterID", connstr);
                        break;
                    case 10:
                        mKInterID = GetMaxAutoID("y_Pra_Master", "KInterID", connstr);
                        break;
                }

                SqlConnection con = new SqlConnection(connstr);
                con.Open();
                SqlTransaction tran = con.BeginTransaction();
                try
                {

                    msql = "Delete t_BillData_Master where FTimestamp = @FTimestamp and FUserID = @FUserID and FTransferFlag = 0 "
                        + "Delete t_BillData_Detail where mFTimestamp = @FTimestamp";
                    SqlCommand cmd2 = new SqlCommand(msql, con);
                    cmd2.Connection = con;
                    cmd2.Transaction = tran;
                    cmd2.Parameters.Clear();
                    if (cmd2 != null) DBHelper.SetArgs(msql, row_master, cmd2);
                    cmd2.ExecuteNonQuery();

                    string msql_master = @"Insert into t_BillData_Master(FTimestamp,KInterID,FBillNo,FDate, FUserID, FCorpID, FStockID, FObjectID,FDataType,
                        FBatchID, FTransferFlag) values(@FTimestamp,{0},'{1}',Getdate(),@FUserID, @FCorpID, @FStockID,@FObjectID, @FDataType, 0,0)";

                    msql_master = string.Format(msql_master, mKInterID, sBillNo);

                    SqlCommand cmd0 = new SqlCommand(msql_master, con);
                    cmd0.Connection = con;
                    cmd0.Transaction = tran;
                    cmd0.Parameters.Clear();
                    if (cmd0 != null) DBHelper.SetArgs(msql_master, row_master, cmd0);
                    cmd0.ExecuteNonQuery();

                    msql = @"Insert into t_BillData_Detail(mFTimestamp,mFInterID,mFRowNo,mFGoodID, mFSubItemID, mFQty, mFPrice, mFAmount, mFDataType, mFBatchID, "
                        + "mFTransferFlag)values({2},{0},@mFRowNo,@FItemID,@FItemID, @FQty, @FPrice,@FAmount, {1},0,0)";
                    msql = string.Format(msql, mKInterID, mDataType, sFTimestamp);

                    int mFRowNo = 1;
                    foreach (Hashtable row_detail in rows_detail)
                    {
                        row_detail["mFRowNo"] = mFRowNo;
                        SqlCommand cmd1 = new SqlCommand(msql, con);
                        cmd1.Connection = con;
                        cmd1.Transaction = tran;
                        cmd1.Parameters.Clear();
                        if (row_detail != null) DBHelper.SetArgs(msql, row_detail, cmd1);
                        cmd1.ExecuteNonQuery();
                        mFRowNo += 1;
                    }

                    msql = "Exec pr_ic_SetBillTransfer @FTimestamp,@KInterID,@FDataType";
                    SqlCommand cmd4 = new SqlCommand(msql, con);
                    cmd4.Connection = con;
                    cmd4.Transaction = tran;
                    cmd4.Parameters.Clear();
                    cmd4.Parameters.Add("FTimestamp", SqlDbType.BigInt).Value = sFTimestamp;
                    cmd4.Parameters.Add("KInterID", SqlDbType.BigInt).Value = mKInterID;
                    cmd4.Parameters.Add("FDataType", SqlDbType.Int).Value = mDataType;
                    cmd4.ExecuteNonQuery();

                    tran.Commit();

                    mBillOrder.KInterID = 0;
                    mBillOrder.FBillNo = "";

                    if (CheckBillSave(sFTimestamp, mCorpID, mUserID, ref mBillOrder))
                    {
                        mBillOrder.KInterID = mKInterID;
                        mBillOrder.FBillNo = sBillNo;
                        return mKInterID;
                    }
                    else
                    {
                        return 0;
                    } 
                }
                catch (SqlException ex)
                {
                    tran.Rollback();
                    return 0;
                }
                finally
                {
                    con.Close();
                    con = null;
                }
            }
            catch (SqlException ex)
            {
                msg = "错误：" + ex.Message.Replace("\"", "").Replace("'", "");
                return 0;
            }
        }

        /// <summary>
        /// 会员资料保存
        /// </summary>
        /// <returns></returns>
        public Int64 SetBillCheck(PrPosDataRequest request, ref BillOrderModel mBillOrder)
        {            
            try
            {
                string dataInfo = Common.ConvertBase64StringToString(request.DataInfo);
                ArrayList rows_master = (ArrayList)PluSoft.Utils.JSON.Decode(Common.GetValidJson(dataInfo));
                if (rows_master.Count == 0) return 0;

                Hashtable row_master = rows_master[0] as Hashtable;

                Int64 mUserID = CommonFun.ConvertToBigInt(row_master["FUserID"]);
                Int64 sBillID = CommonFun.ConvertToBigInt(row_master["KInterID"]);
                int sDataType = CommonFun.ConvertToInt(row_master["FDataType"]);
                int mCoprID = CommonFun.ConvertToInt(row_master["FCorpID"]);
                string connstr = SystemInfo.GetClusterConn(mCoprID);
                string sql = string.Format("EXEC pr_ic_CheckBillData {0},{1},{2},{3}", sBillID, mUserID, mCoprID, sDataType);
                
                ArrayList datarows = DBHelper.Select(sql, connstr);

                int mCheckBillID = 0;
                if (datarows.Count > 0)
                {
                    Hashtable hb = datarows[0] as Hashtable;
                    mCheckBillID = CommonFun.ConvertToInt(hb["KInterID"]);
                    if (mCheckBillID == sBillID)
                    {
                        mBillOrder.KInterID = mCheckBillID;
                        mBillOrder.FBillNo = "";
                        return mCheckBillID;
                    }
                    else
                    {
                        return 0;
                    }
                }
                return 0;
            }
            catch (SqlException ex)
            {
                return 0;
            }
        }


        /// <summary>
        /// 数据查询
        /// </summary>
        /// <returns></returns>
        public ArrayList GetBaseInfo(PrPosDataRequest request, ref bool bResult)
        {
            try
            {
                string dataInfo = Common.ConvertBase64StringToString(request.DataInfo);
                ArrayList sDataRows = (ArrayList)PluSoft.Utils.JSON.Decode(Common.GetValidJson(dataInfo));
                Hashtable row = sDataRows[0] as Hashtable;
                var msql = @"exec pr_ic_SelectItems @mPageSize,@mPageIndex,@mCorpID,@mBillType,@mName,@mClassID";
                string connstr = SystemInfo.GetClusterConn(CommonFun.ConvertToInt(row["FCorpID"]));

                SqlParameterList par_list = new SqlParameterList();
                par_list.Add("@mPageIndex", SqlDbType.Int, CommonFun.ConvertToInt(row["FPageIndex"]));
                par_list.Add("@mPageSize", SqlDbType.Int, 500);
                par_list.Add("@mCorpID", SqlDbType.BigInt, CommonFun.ConvertToBigInt(row["FCorpID"]));
                par_list.Add("@mClassID", SqlDbType.BigInt, 0);
                par_list.Add("@mName", SqlDbType.VarChar, CommonFun.ConvertToString(row["FNumber"]));
                par_list.Add("@mBillType", SqlDbType.VarChar, CommonFun.ConvertToString(row["FDataType"]));

                ArrayList mdata = DBHelper.Select(msql, par_list.Get_List(), connstr);
                if (mdata.Count > 0) bResult = true;
                return mdata;
            }
            catch (Exception ex)
            {
                //Service.HandlerErr(ex.Message);
                return null;
                //WriteError(ex.Message);
            }
        }

        /// <summary>
        /// 商品资料保存
        /// </summary>
        /// <returns></returns>
        public ArrayList SetGoodsInfo(GoodsModel row,String sGoodsImgUrl, ref bool bResult)
        {
            try
            {
                var msql = @"Exec [dbo].[pr_cp_SetGoodsInfo] @FItemID,@FClassID,@FCorpID,@FUserID,@FCode,@FNumber,@FName,@FSpec,"
                    + "@FUnitName,@FPurPrice,@FPosPrice,@FSelPrice,@FRmoPrice,@FVipPrice,@FPoint,@FPriceType,@FGoodsImg";
                string connstr = SystemInfo.GetClusterConn(row.FCorpID);
                SqlParameterList detail_par_list = new SqlParameterList();
                detail_par_list.Add("@FItemID", SqlDbType.BigInt, CommonFun.ConvertToBigInt(row.FItemID));
                detail_par_list.Add("@FClassID", SqlDbType.Int, CommonFun.ConvertToInt(row.FClassID));
                detail_par_list.Add("@FCorpID", SqlDbType.Int, CommonFun.ConvertToInt(row.FCorpID));
                detail_par_list.Add("@FUserID", SqlDbType.BigInt, CommonFun.ConvertToBigInt(row.FUserID));
                detail_par_list.Add("@FCode", SqlDbType.VarChar, CommonFun.ConvertToString(row.FCode));
                detail_par_list.Add("@FNumber", SqlDbType.VarChar, CommonFun.ConvertToString(row.FNumber));
                detail_par_list.Add("@FName", SqlDbType.VarChar, CommonFun.ConvertToString(row.FName));
                detail_par_list.Add("@FSpec", SqlDbType.VarChar, CommonFun.ConvertToString(row.FSpec));
                detail_par_list.Add("@FUnitName", SqlDbType.VarChar, CommonFun.ConvertToString(row.FUnitName));
                detail_par_list.Add("@FPurPrice", SqlDbType.Decimal, CommonFun.ConvertToDecimal(row.FPurPrice));
                detail_par_list.Add("@FPosPrice", SqlDbType.Decimal, CommonFun.ConvertToDecimal(row.FPosPrice));
                detail_par_list.Add("@FSelPrice", SqlDbType.Decimal, CommonFun.ConvertToDecimal(row.FSelPrice));
                detail_par_list.Add("@FRmoPrice", SqlDbType.Decimal, CommonFun.ConvertToDecimal(row.FRmoPrice));
                detail_par_list.Add("@FVipPrice", SqlDbType.Decimal, CommonFun.ConvertToDecimal(row.FVipPrice));
                detail_par_list.Add("@FPoint", SqlDbType.Decimal, CommonFun.ConvertToDecimal(row.FPoint));
                detail_par_list.Add("@FPriceType", SqlDbType.Int, CommonFun.ConvertToInt(row.FPriceType));
                detail_par_list.Add("@FGoodsImg", SqlDbType.VarChar, CommonFun.ConvertToString(sGoodsImgUrl));
                ArrayList mdata = DBHelper.Select(msql, detail_par_list.Get_List(), connstr);
                if (mdata.Count > 0) bResult = true;
                return mdata;
            }
            catch (Exception ex)
            {
                //Service.HandlerErr(ex.Message);
                return null;
                //WriteError(ex.Message);
            }
        }

        /// <summary>
        /// 库存查询
        /// </summary>
        /// <returns></returns>
        public ArrayList GetStockTotal(PrPosDataRequest request, ref bool bResult)
        {
            try
            {
                string dataInfo = Common.ConvertBase64StringToString(request.DataInfo);
                //Service.HandlerErr(dataInfo);
                ArrayList sDataRows = (ArrayList)PluSoft.Utils.JSON.Decode(Common.GetValidJson(dataInfo));
                Hashtable row = sDataRows[0] as Hashtable;
                var msql = @"exec pr_Rpt_GetStockTotalTime @mPageSize,@mPageIndex,@mCorpID,@mStockID,@mClassID,@mBrandID,@mNumber,@mDataType";

                SqlParameterList par_list = new SqlParameterList();
                par_list.Add("@mPageSize", SqlDbType.Int, CommonFun.mRptPageSize);
                par_list.Add("@mPageIndex", SqlDbType.Int, 0);
                par_list.Add("@mCorpID", SqlDbType.Int, CommonFun.ConvertToInt(row["FCorpID"]));
                par_list.Add("@mStockID", SqlDbType.BigInt, CommonFun.ConvertToInt(row["FStockID"]));
                par_list.Add("@mClassID", SqlDbType.BigInt, CommonFun.ConvertToBigInt(row["FClassID"]));
                par_list.Add("@mBrandID", SqlDbType.BigInt, 0);
                par_list.Add("@mNumber", SqlDbType.VarChar, CommonFun.ConvertToString(row["FNumber"]));
                par_list.Add("@mDataType", SqlDbType.Int, 4);
                string connstr = SystemInfo.GetClusterConn(CommonFun.ConvertToInt(row["FCorpID"]));
                ArrayList mdata = DBHelper.Select(msql, par_list.Get_List(), connstr);
                if (mdata.Count > 0) bResult = true;
                return mdata;
            }
            catch (Exception ex)
            {
                //Service.HandlerErr(ex.Message);
                return null;
            }
        }

        /// <summary>
        /// 零售汇总表
        /// </summary>
        /// <returns></returns>
        public ArrayList GetPosDataSum(PrPosDataRequest request, ref bool bResult, ref int sPageIndex)
        {
            try
            {
                string dataInfo = Common.ConvertBase64StringToString(request.DataInfo);
                //Service.HandlerErr(dataInfo);
                ArrayList sDataRows = (ArrayList)PluSoft.Utils.JSON.Decode(Common.GetValidJson(dataInfo));
                Hashtable row = sDataRows[0] as Hashtable;
                var msql = @"exec pr_Rpt_GetPosProfit @mPageSize,@mPageIndex,@mDate1,@mDate2,@mCorpID,@mStockID,@mListType";

                sPageIndex = CommonFun.ConvertToInt(row["FPageIndex"]);
                SqlParameterList par_list = new SqlParameterList();
                par_list.Add("@mPageSize", SqlDbType.Int, CommonFun.mRptPageSize);
                par_list.Add("@mPageIndex", SqlDbType.Int, sPageIndex);
                par_list.Add("@mCorpID", SqlDbType.Int, CommonFun.ConvertToInt(row["FCorpID"]));
                par_list.Add("@mStockID", SqlDbType.BigInt, CommonFun.ConvertToInt(row["FStockID"]));
                par_list.Add("@mDate1", SqlDbType.VarChar, CommonFun.ConvertToString(row["FDate1"]));
                par_list.Add("@mDate2", SqlDbType.VarChar, CommonFun.ConvertToString(row["FDate2"]));
                par_list.Add("@mListType", SqlDbType.Int, CommonFun.ConvertToInt(row["FListType"]));
                string connstr = SystemInfo.GetClusterConn(CommonFun.ConvertToInt(row["FCorpID"]));
                ArrayList mdata = DBHelper.Select(msql, par_list.Get_List(), connstr);
                if (mdata.Count > 0) bResult = true;
                return mdata;
            }
            catch (Exception ex)
            {
                //Service.HandlerErr(ex.Message);
                return null;
            }
        }

        /// <summary>
        /// 零售汇总表
        /// </summary>
        /// <returns></returns>
        public ArrayList GetBillDataList(PrPosDataRequest request, ref bool bResult, ref int sPageIndex)
        {
            try
            {
                string dataInfo = Common.ConvertBase64StringToString(request.DataInfo);
                //Service.HandlerErr(dataInfo);
                ArrayList sDataRows = (ArrayList)PluSoft.Utils.JSON.Decode(Common.GetValidJson(dataInfo));
                Hashtable row = sDataRows[0] as Hashtable;
                var msql = @"exec pr_Rpt_GetBllList	@mPageSize,@mPageIndex,@mDate1,@mDate2,@mCorpID,@mCheckFlag,@mBillType,@mStockID,@mObjectID,@mBillNo,@mListType";

                sPageIndex = CommonFun.ConvertToInt(row["FPageIndex"]);
                SqlParameterList par_list = new SqlParameterList();
                par_list.Add("@mPageSize", SqlDbType.Int, CommonFun.mRptPageSize);
                par_list.Add("@mPageIndex", SqlDbType.Int, sPageIndex);
                par_list.Add("@mCorpID", SqlDbType.Int, CommonFun.ConvertToInt(row["FCorpID"]));
                par_list.Add("@mStockID", SqlDbType.BigInt, CommonFun.ConvertToInt(row["FStockID"]));
                par_list.Add("@mCheckFlag", SqlDbType.Int, CommonFun.ConvertToInt(row["FCheckFlag"]));
                par_list.Add("@mBillType", SqlDbType.Int, CommonFun.ConvertToInt(row["FBillType"]));
                par_list.Add("@mObjectID", SqlDbType.Int, CommonFun.ConvertToInt(row["FObjectID"]));
                par_list.Add("@mDate1", SqlDbType.VarChar, CommonFun.ConvertToString(row["FDate1"]));
                par_list.Add("@mDate2", SqlDbType.VarChar, CommonFun.ConvertToString(row["FDate2"]));
                par_list.Add("@mBillNo", SqlDbType.VarChar, CommonFun.ConvertToString(row["FBillNo"]));
                par_list.Add("@mListType", SqlDbType.Int, CommonFun.ConvertToInt(row["FListType"]));
                string connstr = SystemInfo.GetClusterConn(CommonFun.ConvertToInt(row["FCorpID"]));
                ArrayList mdata = DBHelper.Select(msql, par_list.Get_List(), connstr);
                if (mdata.Count > 0) bResult = true;
                return mdata;
            }
            catch (Exception ex)
            {
                //Service.HandlerErr(ex.Message);
                return null;
            }
        }

    }
}